package com.hx.staticState.demo1;

import java.util.Date;

/**
 * @Author： Huathy
 * @ClassPath： com.hx.staticState.demo1.Cloner
 * @Date： 2021-02-01 17:12
 * @Description： 克隆者
 */
public class Cloner {
    public static void main(String[] args) throws CloneNotSupportedException {
//        //浅拷贝
//        test1();
//        //描述浅拷贝问题
//        test2();
//        //实现深拷贝
        test3();
    }

    /**
     * 深拷贝
     */
    static void test3() throws CloneNotSupportedException {
        //原型对象
        Date date = new Date();
        Video2 v1 = new Video2("嘻嘻", date);
        Video2 v2 = (Video2) v1.clone();

        System.out.println("V1 => " + v1);
        System.out.println("V2 => " + v2);
        System.out.println("=======================");
        date.setTime(123456789);
        System.out.println("V1 => " + v1);
        System.out.println("V2 => " + v2);
    }

    /**
     * 描述浅拷贝问题。浅拷贝的对象，拷贝了原型的引用。
     */
    static void test2() throws CloneNotSupportedException {
        //原型对象
        Date date = new Date();
        Video v1 = new Video("嘻嘻", date);
        Video v2 = (Video) v1.clone();

        System.out.println("V1 => " + v1);
        System.out.println("V2 => " + v2);
        System.out.println("=======================");
        date.setTime(123456789);
        System.out.println("V1 => " + v1);
        System.out.println("V2 => " + v2);
    }

    /**
     * 浅拷贝
     */
    static void test1() throws CloneNotSupportedException {
        //原型对象
        Date date = new Date();
        Video v1 = new Video("嘻嘻", date);
        System.out.println("V1 => " + v1);
        System.out.println("V1-Hash => " + v1.hashCode());

        //克隆对象
        Video v2 = (Video) v1.clone();
        v2.setName("克隆的嘻嘻");
        System.out.println("V2 => " + v2);
        System.out.println("V2-Hash => " + v2.hashCode());
    }
}
